﻿using db_manage.dll.common;
using db_manage.dll.manage;
using db_manage.model;
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;

namespace db_manage.page
{
    public partial class form_bak : Form
    {
        private static form_db_info form_db_info { get => manage.from_get<form_db_info>(); }

        private db_config_bak db_config_bak
        {
            get
            {
                return manage.db_manage_config?.db_config_bak;
            }
        }

        public form_bak()
        {
            InitializeComponent();
            this.cb_content.DataSource = Enum.GetValues(typeof(db_config_bak_content));
            config_get();
            this.dgv_db_config.DataSource = new BindingList<db_link_info>(manage.db_manage_config.db_link_info);
            ((DataGridViewComboBoxColumn)this.dgv_db_config.Columns["type"]).DataSource = Enum.GetValues(typeof(db_type));
            timer_bak_check.Start();
        }

        /// <summary>
        /// 获取配置
        /// </summary>
        private void config_get()
        {
            cb_enable.Checked = db_config_bak.enable;
            cb_content.Text = db_config_bak.content.ToString();
        }

        /// <summary>
        /// 设置配置
        /// </summary>
        private void config_set()
        {
            db_config_bak.enable = cb_enable.Checked;
            db_config_bak.content = (db_config_bak_content)Enum.Parse(typeof(db_config_bak_content), cb_content.Text);
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_save_Click(object sender, EventArgs e)
        {
            config_set();
            manage.db_manage_config = manage.db_manage_config;
            MessageBox.Show("操作完成");
        }

        /// <summary>
        /// 重新运行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_restart_Click(object sender, EventArgs e)
        {
            db_bak();
        }

        public void db_bak()
        {
            this.l_status.Text = "运行状态：运行中";
            form_db_info.ShowAndBringToFront();
            config_set();
            var res = manage.db_bak(null);
            if (!string.IsNullOrEmpty(res))
            {
                MessageBox.Show(res);
            }
            if (res != "已启动备份")
            {
                this.l_status.Text = "运行状态：未运行";
            }
        }

        /// <summary>
        /// 配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgv_db_config_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv_db_config.Columns[e.ColumnIndex].Name == "操作" && e.RowIndex >= 0)
            {
                var nowtip = Convert.ToString(dgv_db_config.CurrentRow.Cells["tip"].Value);
                var db_link_info = manage.db_manage_config.db_link_info?.FirstOrDefault(x => x.tip == nowtip);
                if (db_link_info == null)
                {
                    MessageBox.Show("数据异常，服务器数据不存在");
                    return;
                }

                var db_config_bak_sub = db_link_info.db_config_bak_sub;
                if (db_config_bak_sub == null)
                {
                    db_config_bak_sub = new db_config_bak_sub()
                    {
                        time_start = DateTime.Now.Date,
                        time_end = DateTime.Now.Date,
                        interval_full = 28800,
                        interval_diff = 0
                    };
                    db_link_info.db_config_bak_sub = db_config_bak_sub;
                }
                new form_bak_sub(db_config_bak_sub).ShowAndBringToFront();
            }
        }

        /// <summary>
        /// 检查数据库备份时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_bak_check_Tick(object sender, EventArgs e)
        {
            manage.bak_time_check();
        }


        /// <summary>
        /// 缩小为托盘
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void form_bak_FormClosing(object sender, FormClosingEventArgs e)
        {
            //取消关闭窗口
            e.Cancel = true;
            this.Hide();
        }
    }
}
